<!DOCTYPE html>
<HTML>
<HEAD>
	<TITLE> ZTREE DEMO - checkbox</TITLE>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" href="zTree/css/demo.css" type="text/css">
	<link rel="stylesheet" href="zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
	<!-- <script type="text/javascript" src="../zTree/js/jquery-1.4.4.min.js"></script> -->
	<script type="text/javascript" src="resources/scripts/jquery-1.6.min.js"></script>
	<script type="text/javascript" src="zTree/js/jquery.ztree.core-3.0.js"></script>
	<script type="text/javascript" src="zTree/js/jquery.ztree.excheck-3.0.js"></script>
	<SCRIPT type="text/javascript">
		<!--
		var d;
		var setting = {
			check: {
				enable: true
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				beforeCheck: beforeCheck
			}
		};
		
		function beforeCheck(treeId, treeNode) {
			//alert("id:" + treeNode.id + ",name:" + treeNode.name + ",pId:" + treeNode.pId + ",checked:" + treeNode.checked);
			if(treeNode.checked){
				if(treeNode.pId){//alert("删除子节点");
					for(var i=0;i<d.length;i++){
						var o = d[i];
						if(o){
							if(treeNode.id == o.id)delete d[i];//d.splice(i,1);
						}
					}
				}else{//alert("删除主节点");
					for(var i=0;i<d.length;i++){
						var o = d[i];
						if(o){
							if(treeNode.id == o.pId)delete d[i];//d.splice(i,1);
							if(treeNode.id == o.id)delete d[i];//d.splice(i,1);
						}	
					}
				}
			}else{
				//alert("添加节点");
				var obj = new Object();
				obj.id = treeNode.id;
				obj.name = treeNode.name;
				obj.pId = treeNode.pId;
				d.push(obj,d.length); 
			}
			
		}	
		function count() {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			var checkCount = zTree.getCheckedNodes(true);
			for(var i=0;i<checkCount.length;i++){
				var o = checkCount[i];
				//alert("id:" + o.id + ",name:" + o.name + ",pId:" + o.pId + ",checked:" + o.checked);
			}
		}

		$(document).ready(function(){
			$.getJSON("jsoncompAction.action?id=2",function(data){
				d = data.request;
				var zNodes = data.request;
				$.fn.zTree.init($("#treeDemo"), setting, zNodes);
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				zTree.setting.check.chkboxType = { "Y":"ps", "N":"ps"};
			});
		});
		//-->
	</SCRIPT>
</HEAD>

<BODY>
		<input id="getMessage" type="button" onclick="count()" value="结果"/>
		<ul id="treeDemo" class="ztree"></ul>
</BODY>
</HTML>